package com.tianqingwl.gsystem.front.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.content.param.ContentInfoQueryParam;
import com.tianqingwl.gsystem.content.pojo.ContentInfo;
import com.tianqingwl.gsystem.content.pojo.ContentType;
import com.tianqingwl.gsystem.content.service.ContentInfoService;
import com.tianqingwl.gsystem.content.service.ContentTypeService;
import com.tianqingwl.gsystem.front.service.FrontService;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import com.tianqingwl.gsystem.template.pojo.SysTemplate;
import com.tianqingwl.gsystem.template.service.SysTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class FrontIndexController {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private ContentInfoService contentInfoService;
    @Autowired
    private ContentTypeService contentTypeService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private SysTemplateService sysTemplateService;

    @GetMapping({"/", "", "/page_{page}.html"})
    public String index(Model model, @PathVariable(value = "page", required = false) Long page) {
        page = page == null ? 1 : page;
        Integer size = 15;
        Page<ContentInfo> contentInfoPage = new Page<>(page,size);
        ContentInfoQueryParam param = new ContentInfoQueryParam();
        param.setStatus(1);
        contentInfoService.list(contentInfoPage, param);
        model.addAttribute("page", contentInfoPage);
        //公共信息：导航条
        frontService.commonInfo(model);
        SysTemplate sysTemplate = sysTemplateService.getCurrentTemplate();
        return sysTemplate.getTemplateDir() + "/index";
    }

    @GetMapping({"/{contentId}.html"})
    public String view(@PathVariable("contentId") Long contentId, Model model) {
        ContentInfo contentInfo = contentInfoService.getContentInfoById(contentId);
        if (null == contentInfo) {
            //404
            return "redirect:/error/404.html";
        }
        model.addAttribute("contentInfo", contentInfo);
        //通用信息
        frontService.commonInfo(model);
        //推荐文章
        List<ContentInfo> recommendContentList = contentInfoService.listRecommendContent(contentInfo.getContentType());
        model.addAttribute("recommendContentList", recommendContentList);
        //上一篇、下一篇
        List<ContentInfo> nearContentList = contentInfoService.nearContentList(contentId);
        model.addAttribute("nearContentList", nearContentList);
        //文章标签
        Page<ContentType> tagPage = new Page<>(1, 10);
        contentTypeService.listContentTypePage(tagPage, contentId);
        model.addAttribute("tags", tagPage.getRecords());
        //更新阅读量
        contentInfoService.updateViewCount(contentId);
        SysTemplate sysTemplate = sysTemplateService.getCurrentTemplate();
        return sysTemplate.getTemplateDir() + "/view";
    }

    @GetMapping("/search")
    public String search(String searchContent,
                         Long page,
                         Model model) {
        page = page == null ? 1 : page;
        //搜索功能，支持标题的模糊搜索
        Page<ContentInfo> searchPage = new Page<>(page, 15);
        contentInfoService.search(searchPage, searchContent);
        model.addAttribute("page", searchPage);
        ContentType contentType = new ContentType();
        contentType.setTypeName(searchContent+" 搜索结果");
        contentType.setTitle(searchContent+" 搜索结果");
        model.addAttribute("contentType", contentType);
        model.addAttribute("searchContent",searchContent);
        frontService.commonInfo(model);
        SysTemplate sysTemplate = sysTemplateService.getCurrentTemplate();
        return sysTemplate.getTemplateDir() + "/list";
    }

    @GetMapping({"/list/{typeId}_{page}.html"})
    public String list(@PathVariable("typeId") Long typeId, @PathVariable("page") Long page, Model model) {
        page = page == null ? 1 : page;
        Integer size = 15;
        ContentType contentType = contentTypeService.getContentTypeByTypeAndId(typeId, 1);
        if (null == contentType) {
            return "redirect:/error/404.html";
        }
        Page<ContentInfo> contentInfoPage = new Page<>(page, size);
        ContentInfoQueryParam queryParam = new ContentInfoQueryParam();
        queryParam.setContentType(typeId);
        queryParam.setType(contentType.getType());
        queryParam.setStatus(1);
        contentInfoService.list(contentInfoPage, queryParam);
        model.addAttribute("page", contentInfoPage);

        model.addAttribute("contentType", contentType);
        frontService.commonInfo(model);
        SysTemplate sysTemplate = sysTemplateService.getCurrentTemplate();
        return sysTemplate.getTemplateDir() + "/list";
    }

    @GetMapping({"/tag/{typeId}_{page}.html"})
    public String tag(@PathVariable("typeId") Long typeId, @PathVariable("page") Long page, Model model) {
        page = page == null ? 1 : page;
        Integer size = 15;
        ContentType contentType = contentTypeService.getContentTypeByTypeAndId(typeId, 2);
        if (null == contentType) {
            return "redirect:/error/404.html";
        }
        Page<ContentInfo> contentInfoPage = new Page<>(page, size);
        ContentInfoQueryParam queryParam = new ContentInfoQueryParam();
        queryParam.setContentType(typeId);
        queryParam.setType(contentType.getType());
        queryParam.setStatus(1);
        contentInfoService.list(contentInfoPage, queryParam);
        model.addAttribute("page", contentInfoPage);

        model.addAttribute("contentType", contentType);
        frontService.commonInfo(model);
        SysTemplate sysTemplate = sysTemplateService.getCurrentTemplate();
        return sysTemplate.getTemplateDir() + "/tag";
    }


}
